/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.dc.tool;

import akka.actor.ActorRef;
import com.cxhd.dc.DataConfig;
import com.cxhd.dc.actor.ActorManager;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;

/**
 *
 * @author Administrator
 */
public class StreamGobbler extends Thread {

    private InputStream is;
    private String type;
    private OutputStream os;
    private boolean flag = true;
    private String path;
    private String device;
    ActorManager actorManager = ActorManager.getIntance();

    public StreamGobbler(InputStream is, String type, String path, String device) {

        this(is, type, null, path, device);
    }

    StreamGobbler(InputStream is, String type, OutputStream redirect, String path, String device) {
        this.is = is;
        this.type = type;
        this.os = redirect;
        this.path = path;
        this.device = device;
    }

    public void run() {
        InputStreamReader isr = null;
        BufferedReader br = null;
        PrintWriter pw = null;
        try {

            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {

                System.out.println(type + ">" + line);
                if (type.equalsIgnoreCase("ERROR")) {
                    flag = false;
                }
            }

        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {

                br.close();
                isr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            checkDelete();
        }
    }

    private void checkDelete() {
       
        if (type.equalsIgnoreCase("ERROR")) {

            if (flag) {
                File sendFile = new File(path);
                if (DataConfig.getInstance().getCacheServer() != null) {
                    actorManager.getActor("SendCacheServerActor").tell(device + "@" + sendFile.getAbsolutePath(), ActorRef.noSender());
                } else {
                    for (File del : sendFile.listFiles()) {
                        //System.out.println("K:/dicomTest/" + del.getName());

                        del.delete();
                    }
                }
                sendFile.delete();
//                            newFloder.renameTo(new File(ApProperties.getBasePath() + "sendBackup/" + newFloder.getName()));
//                            list.remove(newFloder.getAbsolutePath());
//
//                            removeDirectory(newFloder);

            } else {
                System.out.println("删除文件");
            }
        }
    }
}
